//
//  ViewController.m
//  requestData
//
//  Created by MyMac on 15/8/7.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//
#define NavagationHeader @"<TeacherId></TeacherId><OrgId>%@</OrgId>"
#import "ViewController.h"
#import "LJRequestData.h"
#define POSTParms @"\
<Root>\
<BizCode>%@</BizCode>\
<ClientType>IPHONE</ClientType>\
<ClientOS>iPhone_OS</ClientOS>\
<ClientIP></ClientIP>\
<Award></Award>\
<SessionId></SessionId>\
<SvcContent>\
<![CDATA[\
<Request>\%@</Request>\
]]>\
</SvcContent>\
</Root>"
#import "AppDelegate.h"
//NSURLConnectionDataDelegate是基于NSURLConnectionDelegate的
@interface ViewController ()<NSURLConnectionDelegate,UIAlertViewDelegate >
{
    NSURLRequest* _request;
    
    NSURLConnection *  connection;
    
    NSString* filePath;
    
    NSOutputStream *  fileStream;
    
    NSURL* url,*baseUrl;
    
    NSStringEncoding enc;
    
    NSURLAuthenticationChallenge *_challenge;





}
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UIButton *GET;
@property(nonatomic,strong)LJRequestData*requestLJ;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIImageView*imageview=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
//    imageview.backgroundColor=[UIColor redColor];
   // [self.view addSubview:imageview];
    self.requestLJ=[[LJRequestData alloc]init];
    [self.requestLJ systemHTTPSRequest:^(id data) {
//        NSLog(@"%@",[NSThread currentThread]);
//        UIImage*image=[UIImage imageWithData:data];
//        imageview.image = image;
    }]   ;
//    NSString*request=[NSString stringWithFormat:NavagationHeader,@""];
//    NSDictionary * labelDic = @{@"xmlmsg": [NSString stringWithFormat:POSTParms,@"KJ10005",request]};
//    
//    [LJRequestData afPostdataUrl:@"http://116.213.210.67:7001/kjgl/Service" andparam:labelDic finishedBlock:^(id complete) {
//        NSLog(@"%@",complete);
//    } failedBolck:^(id failed) {
//        NSLog(@"%@",failed);
//    }];
//    
//    [LJRequestData requestDownDataUrl:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4" andfileName:nil andfileType:@"mp4" andfinishedBlock:^(id complete) {
//        NSLog(@"%@",complete);
//    } andfailed:^(id failed) {
//        NSLog(@"%@",failed);
//    } ];







}
- (IBAction)btnCLICK:(id)sender {
//    _challenge=nil;
//    //AppDelegate*app=[UIApplication sharedApplication].delegate;
//    NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    filePath = [NSString stringWithFormat:@"%@/%@",str,@"https://www.google.cn"];
//    NSLog(@"filePath=%@",filePath);
//    
//    fileStream = [[NSOutputStream alloc]initToFileAtPath:filePath append:NO];
//    
//    assert(fileStream != nil);
//    
//    
//    
//    [fileStream open];
  
    _request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.cn"]];
    
    assert(_request != nil);
    [self.webView loadRequest:_request];
    
    
    connection = [NSURLConnection connectionWithRequest:_request delegate:self];
    
    
    
    
}
//第一次登陆这个HTTPS请求时才会需要授权验证，，之后再登陆同一个网站就不需要了，，，这几个验证的代理就不走了
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

{
    
    NSLog(@"authenticatemethod:%@",protectionSpace.authenticationMethod);
    
    return [protectionSpace.authenticationMethod isEqualToString:
            
            NSURLAuthenticationMethodServerTrust];
    
}
//- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
//{
//
//
//    NSLog(@"%@",challenge);
//
//}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{
    
    _challenge=challenge;
    
    UIAlertView* alertView = [[UIAlertView  alloc] initWithTitle:@"服务器证书"
                               
                                                        message:@"这个网站有一个服务器证书，点击“接受”，继续访问该网站，如果你不确定，请点击“取消”。"
                               
                                                       delegate:self
                               
                                              cancelButtonTitle:@"接受"
                               
                                              otherButtonTitles:@"取消", nil];
    
    
    
    [alertView show];
    
    
    
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
{
    NSLog(@"%@",error);

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    
    // Accept=0,Cancel=1;
    
    if(buttonIndex==0){
        
        NSURLCredential *   credential;
        
        
        
        NSURLProtectionSpace *  protectionSpace;
        
        SecTrustRef            trust;
        
        NSString *              host;
        
        SecCertificateRef      serverCert;
        
        assert(_challenge !=nil);
        
        protectionSpace = [_challenge protectionSpace];
        
        assert(protectionSpace != nil);
        
        
        
        trust = [protectionSpace serverTrust];
        
        assert(trust != NULL);
        
        
        
        credential = [NSURLCredential credentialForTrust:trust];
        
        assert(credential != nil);
        
        host = [[_challenge protectionSpace] host];
        
        if (SecTrustGetCertificateCount(trust) > 0) {
            
            serverCert = SecTrustGetCertificateAtIndex(trust, 0);
            
        } else {
            
            serverCert = NULL;
            
        }
        
        [[_challenge sender] useCredential:credential forAuthenticationChallenge:_challenge];
        
    }
    
}


@end
